package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.x.erp.payload.CyAuctionNoticeMsg;
import com.x.erp.rpc.*;
import com.x.erp.util.Constant;
import com.x.erp.util.ConstantTopic;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.rocketmq.RocketMqProducer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 推送跳车赢
 */
@RestController
@RequestMapping(value = "/public/openApi")
public class OpenApiController extends BaseController {

	@Autowired
	private RocketMqProducer rocketMqProducer;

    /**
	 * 推送商户车赢的SDK的mq消息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/pushCYAuctionNotice", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel pushCYAuctionNotice(CyAuctionNoticeMsg cyAuctionNoticeMsg, String token, HttpServletRequest request) {
		String ip = getClientIP(request);
		if(!Constant.IP_WHITE_LIST.contains(ip)){
			return new ResultModel(ResultCode.RESULT_FAIL, "该IP无访问权限！");
		}
		if(StringUtils.isEmpty(token) || !token.equals("fb537182d5200428ba25678b5727ff5d")){
			return new ResultModel(ResultCode.RESULT_FAIL, "token错误！");
		}
		if(StringUtils.isEmpty(cyAuctionNoticeMsg.getTitle())){
			return new ResultModel(ResultCode.RESULT_FAIL, "title不能为空！");
		}
		if(StringUtils.isEmpty(cyAuctionNoticeMsg.getContent())){
			return new ResultModel(ResultCode.RESULT_FAIL, "content不能为空！");
		}
		if(StringUtils.isEmpty(cyAuctionNoticeMsg.getPid())){
			return new ResultModel(ResultCode.RESULT_FAIL, "pid不能为空！");
		}
		if(StringUtils.isEmpty(cyAuctionNoticeMsg.getPushMessageTypeId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "pushMessageTypeId不能为空！");
		}
		cyAuctionNoticeMsg.setType("carWin");
		produceRocketMqMessage(ConstantTopic.TOPIC_ERP_CAR, ConstantTopic.TAG_SEND_SDK_TO_SHOPUSER, cyAuctionNoticeMsg, "推送SDK给商户");
		return new ResultModel(ResultCode.RESULT_SUCCESS, "推送成功");
	}

	private void produceRocketMqMessage(String topic, String tag, Object obj, String msg){
		try {
			rocketMqProducer.produceMessage(topic, tag, obj);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(msg + "异常", e);
		}
	}
	private String getClientIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
	}

}
